package com.sdwz.user.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdwz.commonutil.Result;
import com.sdwz.serviceutil.utils.JwtUtil;
import com.sdwz.user.client.LibraryClient;
import com.sdwz.user.entity.BookInfo;
import com.sdwz.user.entity.ShelfInfo;
import com.sdwz.user.service.ShelfInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 借书架表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-29
 */
@RestController
@RequestMapping("/api/user")
public class ShelfApiController {

    @Autowired
    ShelfInfoService shelfInfoService;

    @Autowired
    LibraryClient libraryClient;

    /**
     * 添加图书到书架
     * @param request
     * @param bookId
     * @return
     */
    @PostMapping("/auth/shelf/add/{bookId}")
    public Result addShelf(HttpServletRequest request,
                           @PathVariable String bookId){
        Long userId = JwtUtil.getUserId(request.getHeader("token"));
        QueryWrapper<ShelfInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId).eq("book_id",bookId);
        ShelfInfo info = shelfInfoService.getOne(wrapper);
        if (info != null){
            return Result.fail().message("书架中已存在");
        }
        ShelfInfo shelfInfo = new ShelfInfo();
        shelfInfo.setBookId(bookId);
        shelfInfo.setUserId(String.valueOf(userId));
        shelfInfoService.save(shelfInfo);
        return Result.ok();
    }

    /**
     * 获取书架书籍数量
     * @param request
     * @return
     */
    @GetMapping("/auth/shelf/bookNumber")
    public Result countShelfBook(HttpServletRequest request){
        Long userId = JwtUtil.getUserId(request.getHeader("token"));
        QueryWrapper<ShelfInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        int count = shelfInfoService.count(wrapper);
        Map<String ,Integer> result = new HashMap<>();
        result.put("count",count);
        return Result.ok(result);
    }

    /**
     * 获取书架书籍列表
     * @param request
     * @return
     */
    @GetMapping("/auth/shelf/bookList")
    public Result getShelfBooks(HttpServletRequest request){

        List<Map<String,Object>> result = new ArrayList<>();

        Long userId = JwtUtil.getUserId(request.getHeader("token"));
        QueryWrapper<ShelfInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<ShelfInfo> shelfInfos = shelfInfoService.list(wrapper);
        for (ShelfInfo shelfInfo:shelfInfos){
            Map<String,Object> map = new HashMap<>();
            String bookId = shelfInfo.getBookId();

            BookInfo bookInfo = libraryClient.getBook(bookId);
            if (bookInfo == null){
                delShelfBookById(String.valueOf(userId),bookId);
                continue;
            }

//            BookInfo bookInfo = (BookInfo)bookById.getData();
            map.put("id",shelfInfo.getId());
            map.put("book",bookInfo);
            result.add(map);
        }
        return Result.ok(result);

    }

    /**
     * 删除书架书籍
     * @param ids
     * @return
     */
    @DeleteMapping("/auth/shelf/removeByIds")
    public Result delShelfBooks(@RequestBody List<String> ids){
        System.out.println(ids);
        shelfInfoService.removeByIds(ids);
        return Result.ok();
    }

    /**
     * 删除书架书籍
     * @param bookIds
     * @return
     */
    @DeleteMapping("/auth/shelf/removeByBookIds/{userId}")
    public void delShelfBooksByBookIds(@PathVariable String userId,@RequestBody List<String> bookIds){

        for (String bookId:bookIds){
            delShelfBookById(userId,bookId);
        }

    }

    public void delShelfBookById(String userId,String bookId){
        QueryWrapper<ShelfInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId).eq("book_id",bookId);

        shelfInfoService.remove(wrapper);
    }

}

